import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AiFormTemplatesApi } from '#/api/ai/form/formTemplates';
import type { AiFormVariablesApi } from '#/api/ai/form/formVariables';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE, HTML_TYPE, JAVA_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { IconifyIcon } from '@vben/icons';
import { handleTree } from '@vben/utils';

import { getFormCategoriesList } from '#/api/ai/form/formCategories';
import { getFormTagPage } from '#/api/ai/form/formTag';
import { getFormVariablesList } from '#/api/ai/form/formVariables';
import { getFileCategoriesList } from '#/api/infra/filecategories';
import { getSimpleDictTypeList } from '#/api/system/dict/type';
import { ColorPicker } from '#/components/color-picker';
import { DictBadge } from '#/components/dict-badge';
import { DictTag } from '#/components/dict-tag';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '表单编号',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入表单编号',
      },
    },
    {
      fieldName: 'title',
      label: '表单标题',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入表单标题',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getFormCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择表单分类',
        treeDefaultExpandAll: true,
      },
      rules: 'selectRequired',
    },
    {
      fieldName: 'status',
      label: '状态',
      rules: 'selectRequired',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_Form_STATUS),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'versionNum',
      label: '版本号',
      component: 'Input',
      rules: 'required',
      componentProps: {
        placeholder: '请输入版本号',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'icon',
      label: '图标',
      component: 'IconPicker',
      componentProps: {
        placeholder: '请选择分类图标',
        prefix: 'carbon',
      },
    },
    {
      fieldName: 'color',
      label: '颜色',
      component: 'ColorPicker',
      componentProps: {
        placeholder: '请输入颜色',
        showAlpha: false,
      },
    },
    {
      fieldName: 'tagNameList',
      label: '标签',
      component: 'ApiPagingSelect',
      formItemClass: 'col-span-2',
      componentProps: {
        allowClear: true,
        api: getFormTagPage,
        labelField: 'name',
        valueField: 'name',
        placeholder: '请选择标签',
        mode: 'tags',
      },
    },
    {
      fieldName: 'description',
      label: '描述',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '简要的描述表单的功能和使用场景',
        rows: 4,
      },
    },
    {
      fieldName: 'usageDescription',
      label: '使用说明',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '简要的描述表单的使用说明',
        rows: 4,
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '表单编号',
      field: 'code',
    },
    {
      label: '表单标题',
      field: 'title',
    },
    {
      label: '分类',
      field: 'categoryName',
    },
    {
      label: '版本号',
      field: 'versionNum',
    },
    {
      label: '系统内置',
      field: 'systemFlag',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '排序',
      field: 'sort',
    },
    {
      label: '图标',
      field: 'icon',
      render: (val, data) => {
        return val
          ? h(IconifyIcon, {
              icon: val,
              color: data?.color,
            })
          : '无';
      },
    },
    {
      label: '颜色',
      field: 'color',
      render: (val) => {
        return val
          ? h(ColorPicker, {
              modelValue: val,
              disabled: true,
            })
          : '无';
      },
    },
    {
      label: '状态',
      field: 'status',
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.AI_Form_STATUS,
          value: val,
        }),
    },
    {
      label: '标签',
      field: 'tagNameList',
      span: 2,
      render: (val) =>
        h(DictTag, {
          value: val,
        }),
    },
    {
      label: '表单描述',
      field: 'description',
      span: 2,
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '使用说明',
      field: 'usageDescription',
      span: 2,
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '表单编号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入表单编号',
      },
    },
    {
      fieldName: 'title',
      label: '表单标题',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入表单标题',
      },
    },
    {
      fieldName: 'categoryId',
      label: '分类',
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: async () => {
          const data = await getFormCategoriesList({});
          return handleTree(data);
        },
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择表单分类',
        treeDefaultExpandAll: true,
      },
    },
    {
      fieldName: 'versionNum',
      label: '版本号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入版本号',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.AI_Form_STATUS),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'systemFlag',
      label: '系统内置',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        placeholder: '请选择是否系统内置',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<AiFormTemplatesApi.FormTemplates>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'code',
      title: '表单编号',
      minWidth: 120,
    },
    {
      field: 'title',
      title: '表单标题',
      minWidth: 120,
    },
    {
      field: 'categoryName',
      title: '分类',
      minWidth: 120,
    },
    {
      field: 'versionNum',
      title: '版本号',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.AI_Form_STATUS },
      },
    },
    {
      field: 'systemFlag',
      title: '系统内置',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 列表的字段 */
export function FormDesignGridColumns(): VxeTableGridOptions<AiFormVariablesApi.FormVariables>['columns'] {
  return [
    {
      field: 'parentId',
      title: '上级变量',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'name',
      title: '变量名',
      minWidth: 120,
    },
    {
      field: 'displayName',
      title: '显示名称',
      minWidth: 200,
      treeNode: true,
    },
    {
      field: 'javaType',
      title: '字段类型',
      minWidth: 120,
    },
    {
      field: 'htmlType',
      title: '表单类型',
      minWidth: 120,
    },
    {
      field: 'dataType',
      title: '数据类型',
      minWidth: 120,
    },
    {
      field: 'defaultValue',
      title: '默认值',
      minWidth: 300,
    },
    {
      field: 'required',
      title: '是否必填',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'displayFlag',
      title: '是否显示',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'disabled',
      title: '是否禁用编辑',
      minWidth: 120,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 120,
    },
    {
      field: 'actions',
      title: '操作',
      width: 260,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

/** 新增/修改的表单 */
export function FormDesignFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'formFieldList',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'promptId',
      label: '提示词ID',
      rules: 'required',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'parentId',
      label: '上级变量',
      component: 'ApiTreeSelect',
      defaultValue: 0,
      componentProps(values) {
        return {
          allowClear: true,
          api: async () => {
            const data = await getFormVariablesList({
              promptId: values.promptId,
            });
            data.unshift({
              id: 0,
              displayName: '顶级提示词变量',
            });
            return handleTree(data);
          },
          labelField: 'displayName',
          valueField: 'id',
          childrenField: 'children',
          placeholder: '请选择上级提示词变量',
          treeDefaultExpandAll: true,
        };
      },
      rules: 'selectRequired',
    },

    {
      fieldName: 'name',
      label: '变量名',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入变量名',
      },
    },
    {
      fieldName: 'displayName',
      label: '显示名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入显示名称',
      },
    },
    {
      fieldName: 'javaType',
      label: '字段类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: JAVA_TYPE,
        placeholder: '请选择字段类型',
      },
    },
    {
      fieldName: 'metadata.arrayJavaType',
      label: 'array中类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: JAVA_TYPE.filter((item) => item.value !== 'Array'),
        placeholder: '请选择array中字段类型',
      },
      dependencies: {
        triggerFields: ['metadata', 'javaType'],
        show: (values) => {
          return values.javaType === 'Array';
        },
      },
    },
    {
      fieldName: 'htmlType',
      label: '表单类型',
      rules: 'required',
      component: 'Select',
      componentProps: {
        options: HTML_TYPE,
        placeholder: '请选择表单类型',
      },
    },
    {
      fieldName: 'metadata.isCustomOptions',
      label: '选项类型',
      component: 'Switch',
      componentProps: {
        class: '',
        checkedChildren: '自定义',
        unCheckedChildren: '系统字典',
        size: 'small',
      },
      defaultValue: false,
      dependencies: {
        triggerFields: ['htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'select' ||
            values.htmlType === 'radio' ||
            values.htmlType === 'checkbox'
          );
        },
      },
    },
    {
      fieldName: 'metadata.dictOptions',
      label: '数据字典选项',
      component: 'DictOptionsTable',
      componentProps: {
        placeholder: '请添加数据字典选项',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            (values.metadata?.isCustomOptions ?? false) &&
            (values.htmlType === 'select' ||
              values.htmlType === 'radio' ||
              values.htmlType === 'checkbox')
          );
        },
      },
    },
    {
      fieldName: 'metadata.dictType',
      label: '数据字典类型',
      component: 'ApiSelect',
      componentProps: () => {
        return {
          api: getSimpleDictTypeList,
          placeholder: '请输入字典类型',
          labelField: 'name',
          valueField: 'type',
          showSearch: true,
          optionFilterProp: 'label',
        };
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            !(values.metadata?.isCustomOptions ?? false) &&
            (values.htmlType === 'select' ||
              values.htmlType === 'radio' ||
              values.htmlType === 'checkbox')
          );
        },
      },
    },
    {
      fieldName: 'metadata.dictMode',
      label: '数据字典模式',
      component: 'RadioGroup',
      defaultValue: '',
      componentProps: {
        options: [
          {
            label: '单选',
            value: '',
          },
          {
            label: '多选',
            value: 'multiple',
          },
          {
            label: '标签',
            value: 'tags',
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'select' ||
            values.htmlType === 'radio' ||
            values.htmlType === 'checkbox' ||
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect'
          );
        },
      },
    },
    {
      fieldName: 'metadata.apiUrl',
      label: '接口地址',
      component: 'Input',
      componentProps: {
        placeholder: '请输入接口地址',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'autoComplete' ||
            values.htmlType === 'apiCascader'
          );
        },
      },
    },
    {
      fieldName: 'metadata.apiMethod',
      label: '接口请求方式',
      component: 'Select',
      componentProps: {
        placeholder: '请选择接口请求方式',
        options: [
          {
            label: 'get',
            value: 'get',
          },
          {
            label: 'post',
            value: 'post',
          },
          {
            label: 'put',
            value: 'put',
          },
          {
            label: 'del',
            value: 'del',
          },
          {
            label: 'download',
            value: 'download',
          },
        ],
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'autoComplete' ||
            values.htmlType === 'apiCascader'
          );
        },
      },
    },
    {
      fieldName: 'metadata.apiParams',
      label: '请求参数',
      component: 'ParamConfigTable',
      dependencies: {
        triggerFields: ['metadata', 'htmlType', 'formFieldList'],
        show: (values) => {
          return (
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'autoComplete' ||
            values.htmlType === 'apiCascader'
          );
        },
        componentProps(values) {
          // json转成list
          let formFieldList: any[] = [];
          if (values.formFieldList) {
            try {
              formFieldList = Array.isArray(values.formFieldList)
                ? values.formFieldList
                : JSON.parse(values.formFieldList);
            } catch (error) {
              console.warn('formFieldList 解析失败', error);
            }
          }
          return {
            formFieldList,
            placeholder: '接口请求需要附带的参数',
          };
        },
      },
    },
    {
      fieldName: 'metadata.labelField',
      label: 'label字段',
      component: 'Input',
      componentProps: {
        placeholder: '请输入label字段',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'autoComplete' ||
            values.htmlType === 'apiCascader'
          );
        },
      },
    },
    {
      fieldName: 'metadata.valueField',
      label: 'value字段',
      component: 'Input',
      componentProps: {
        placeholder: '请输入value字段',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'autoComplete' ||
            values.htmlType === 'apiCascader'
          );
        },
      },
    },
    {
      fieldName: 'metadata.childrenField',
      label: 'children字段',
      component: 'Input',
      componentProps: {
        placeholder: '请输入children字段',
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'selectTree' ||
            values.htmlType === 'apiCascader'
          );
        },
      },
    },
    {
      fieldName: 'metadata.minNumber',
      label: '最小上传数量',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入最小上传数量',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'imageUpload' ||
            values.htmlType === 'fileUpload'
          );
        },
      },
    },
    {
      fieldName: 'metadata.maxNumber',
      label: '最大上传数量',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入最大上传数量',
      },
      defaultValue: 1,
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'imageUpload' ||
            values.htmlType === 'fileUpload'
          );
        },
      },
    },
    {
      fieldName: 'metadata.fileCategoriesCode',
      label: '文件分类',
      component: 'ApiSelect',
      componentProps: () => {
        return {
          api: getFileCategoriesList,
          placeholder: '请输入文件分类',
          labelField: 'name',
          valueField: 'code',
          showSearch: true,
          allowClear: true,
        };
      },
      rules: 'required',
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'imageUpload' ||
            values.htmlType === 'fileUpload'
          );
        },
      },
    },
    {
      fieldName: 'metadata.objectId',
      label: '文件对象id',
      component: 'Input',
      componentProps: {
        placeholder: '请输入文件对象id',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'imageUpload' ||
            values.htmlType === 'fileUpload'
          );
        },
      },
    },
    {
      fieldName: 'metadata.objectType',
      label: '文件对象类型',
      component: 'Input',
      componentProps: {
        placeholder: '请输入文件对象类型',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'imageUpload' ||
            values.htmlType === 'fileUpload'
          );
        },
      },
    },
    {
      fieldName: 'metadata.rateCount',
      label: '评分总数',
      component: 'InputNumber',
      componentProps: {
        placeholder: '请输入评分总数',
      },
      rules: 'required',
      defaultValue: 5,
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return values.htmlType === 'rate';
        },
      },
    },
    {
      fieldName: 'metadata.allowHalf',
      label: '是否允许半分',
      rules: 'required',
      defaultValue: true,
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return values.htmlType === 'rate';
        },
      },
    },
    {
      fieldName: 'sort',
      label: '排序',
      rules: 'required',
      component: 'InputNumber',
      componentProps: {
        min: 0,
        placeholder: '请输入排序',
      },
    },
    {
      fieldName: 'required',
      label: '是否必填',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: true,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'displayFlag',
      label: '显示控制',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: true, // 默认“是”
      componentProps: {
        options: [
          {
            label: '是', // 一直显示
            value: true,
          },
          {
            label: '否', // 永远隐藏
            value: false,
          },
          {
            label: '根据字段隐藏', // 动态控制隐藏
            value: 'dynamic',
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'metadata.hiddenByField',
      label: '隐藏关联字段',
      component: 'Select',
      dependencies: {
        triggerFields: ['displayFlag', 'formFieldList'],
        show: (values) => {
          return values.displayFlag === 'dynamic';
        },
        componentProps({ formFieldList }) {
          let options: any[] = [];

          if (formFieldList) {
            try {
              const parsed = Array.isArray(formFieldList)
                ? formFieldList
                : JSON.parse(formFieldList);

              // 转换成 Select 兼容格式
              options = parsed.map((item) => ({
                label: item.label,
                value: item.field,
              }));
            } catch (error) {
              console.warn('[hiddenByField] formFieldList 解析失败:', error);
            }
          }

          return {
            mode: 'multiple',
            allowClear: true,
            placeholder: '请选择关联字段',
            showSearch: true,
            optionFilterProp: 'label',
            options,
          };
        },
      },
    },
    {
      fieldName: 'disabled',
      label: '是否禁用编辑',
      rules: 'required',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'metadata.onChangeFlag',
      label: '是否有Change事件',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          {
            label: '是',
            value: true,
          },
          {
            label: '否',
            value: false,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
      dependencies: {
        triggerFields: ['metadata', 'htmlType'],
        show: (values) => {
          return (
            values.htmlType === 'apiCascader' ||
            values.htmlType === 'apiPagingSelect' ||
            values.htmlType === 'apiSelect' ||
            values.htmlType === 'select' ||
            values.htmlType === 'selectTree' ||
            values.htmlType === 'input' ||
            values.htmlType === 'inputNumber'
          );
        },
      },
    },
    {
      fieldName: 'metadata.onChangeMessage',
      label: 'change事件内容',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '例如:values.versionId = option.id;',
      },
      dependencies: {
        triggerFields: ['onChangeFlag', 'metadata'],
        show: (values) => {
          return values.metadata.onChangeFlag;
        },
      },
    },
    {
      fieldName: 'description',
      label: '填报说明',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入填报说明',
      },
    },
    {
      fieldName: 'defaultValue',
      label: '默认值',
      component: 'Textarea',
      componentProps: {
        rows: 8,
        placeholder: '请输入默认值',
      },
    },

    {
      fieldName: 'example',
      label: '示例值',
      component: 'Textarea',
      componentProps: {
        rows: 8,
        placeholder: '请输入示例值',
      },
    },
    {
      fieldName: 'validationRules',
      label: '验证规则',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入验证规则',
      },
    },
  ];
}

/** 详情页字段 */
export function FormDesignDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '提示词ID',
      field: 'promptId',
    },
    {
      label: '变量名',
      field: 'name',
    },
    {
      label: '显示名称',
      field: 'displayName',
    },
    {
      label: '字段类型',
      field: 'javaTypeName',
    },
    {
      label: '表单类型',
      field: 'htmlTypeName',
    },
    {
      label: '数据类型',
      field: 'dataTypeName',
    },
    {
      label: '默认值',
      field: 'defaultValue',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '填报说明',
      field: 'description',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '是否必填',
      field: 'required',
    },
    {
      label: '验证规则',
      field: 'validationRules',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '排序',
      field: 'sort',
    },
    {
      label: '其他参数',
      field: 'metadata',
      render: (val) => h('div', { innerHTML: val }),
    },
    {
      label: '上级变量id',
      field: 'parentId',
    },
    {
      label: '数据字典选项',
      field: 'dictOptions',
      render: (val) => {
        if (!val) return '-';
        const options = val.split('\n').filter(Boolean);
        return h(
          'div',
          options.map((option) => {
            const [label, value] = option.split('=');
            return h(
              'div',
              { style: 'margin-bottom: 4px;' },
              `${label} = ${value}`,
            );
          }),
        );
      },
    },
  ];
}

/** 列表的搜索表单 */
export function FormDesignGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: '变量名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入变量名',
      },
    },
    {
      fieldName: 'displayName',
      label: '显示名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入显示名称',
      },
    },
  ];
}
